package com.sgb.goods.widget.pop.adapter;

import android.content.Context;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.goods.BR;
import com.sgb.goods.R;
import com.sgb.goods.entity.AttributeViewEntity;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.Arrays;
import java.util.List;

/**
 * 描述：商品属性第一级列表适配器
 *
 * @author qiangHeNoo
 * <p>
 * 时间：2021.12.09
 */
public class GoodsAttributeViewAdapter extends BaseRecycleAdapter<AttributeViewEntity> {

    /**
     * 此处有问题，暂时还原原始逻辑，有待优化
     */
    private final String[] attributeString = new String[20];

    public GoodsAttributeViewAdapter(Context context, List<AttributeViewEntity> datas) {
        super(context, R.layout.goods_item_attribute_view_item, datas);
    }

    @Override
    public void convert(ItemViewHolder holder, final AttributeViewEntity entity) {
        holder.setBinding(BR.viewModel, entity);
        RecyclerView mRecyclerView = holder.getItem().findViewById(R.id.attributeChildRecyclerView);
        GoodsAttributeChildAdapter mAttributeChildAdapter = new GoodsAttributeChildAdapter(context, entity.getType());
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 4);
        mRecyclerView.setLayoutManager(gridLayoutManager);
        mRecyclerView.setAdapter(mAttributeChildAdapter);
        mAttributeChildAdapter.setItemSelect(true);
        mAttributeChildAdapter.setOnItemClick(new AdapterOnItemClick<AttributeViewEntity.TypeBean>() {
            @Override
            public void onItemClick(AttributeViewEntity.TypeBean typeBean, int position) {
                String cid = typeBean.getCid();
                List<AttributeViewEntity> attributeViewEntities = getDatas();
                for (int i = 0; i < attributeViewEntities.size(); i++) {
                    List<AttributeViewEntity.TypeBean> typeBeanList = attributeViewEntities.get(i).getType();
                    for (int j = 0; j < typeBeanList.size(); j++) {
                        if (cid.equals(typeBeanList.get(j).getCid())) {
                            typeBean.setSelected(true);
                            attributeString[i] = entity.getId() + "&" + entity.getName() + "&" + typeBean.getCid() + "&" + typeBean.getCname();
                        } else {
                            typeBean.setSelected(false);
                        }
                    }
                }
            }
        });
    }

    public void clearAttributeString() {
        Arrays.fill(attributeString, "");
    }

    public String[] getAttributeString() {
        return attributeString;
    }
}
